package com.test.hongbao;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import java.util.List;

public class MainActivity extends Activity {

    private final Intent mAccessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
    private final Intent mNotifyIntent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);

    private Button openWechat, openAccessible, button_isopen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openWechat = (Button) findViewById(R.id.button_open_wechat);
        openWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(mNotifyIntent);
            }
        });
        openAccessible = (Button) findViewById(R.id.button_accessible);
        openAccessible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(mAccessibleIntent);
            }
        });
        button_isopen = (Button) findViewById(R.id.button_isopen);
        button_isopen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean serviceEnabled = HongbaoPreference.getInstance(MainActivity.this).isAutoHongbao();
                if (serviceEnabled) {
                    HongbaoPreference.getInstance(MainActivity.this).setAutoHongbao(false);
                } else {
                    HongbaoPreference.getInstance(MainActivity.this).setAutoHongbao(true);
                }
                updateOpenStatus();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(HongbaoFloatService.instace!=null){
            HongbaoFloatService.instace.stopSelf();
        }
        updateServiceStatus();
        updateOpenStatus();
    }

    @Override
    public void onStop(){
        boolean serviceEnabled = HongbaoPreference.getInstance(MainActivity.this).isAutoHongbao();
        if (serviceEnabled) {
            startFloatService();
        }else{
            if(HongbaoFloatService.instace!=null){
                HongbaoFloatService.instace.stopSelf();
            }
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onDestroy();
    }


    private void updateServiceStatus() {
        boolean serviceEnabled = false;
        AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
        List<AccessibilityServiceInfo> accessibilityServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : accessibilityServices) {
            if (info.getId().equals(getPackageName() + "/.HongbaoAccessibilityService")) {
                serviceEnabled = true;
                break;
            }
        }
        if (serviceEnabled) {
            openAccessible.setText("关闭微信列表红包检测服务");
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            openAccessible.setText("开启微信列表红包检测服务");
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }


    private void updateOpenStatus() {
        boolean serviceEnabled = HongbaoPreference.getInstance(this).isAutoHongbao();
        if (serviceEnabled) {
            button_isopen.setText("红包开关：(已打开)");
        } else {
            button_isopen.setText("红包开关：(已关闭)");
        }
    }

    private void startFloatService() {
        Intent in = new Intent(this, HongbaoFloatService.class);
        this.startService(in);
    }


}
